import {Component, OnInit, ViewChild, ViewEncapsulation} from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {ToolsService} from "@shared/service/tools/tools.service";
import {Subject} from "rxjs/Subject";
import {ModalComponent} from "@shared/components/modal/modal.component";
import {SaleService} from "@sale/sale.service";

import {AuthService} from "@shared/service/auth/auth.service";
import {SaleRankVO} from "@interface/index";


@Component({
  selector: 'seed-sale-index',
  templateUrl: './sale-index.component.html',
  styleUrls: ['./sale-index.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class SaleIndexComponent implements OnInit {
  @ViewChild(ModalComponent) modal;
  private ngUnsubscribe: Subject<any> = new Subject<any>();
  public pageData = {
    orgUserId: ''
  };
  public saleInfo: SaleRankVO = new SaleRankVO();

  constructor(private toolsService: ToolsService,
              private service: SaleService,
              public router: Router,
              private route: ActivatedRoute,
              private authService: AuthService) {
  }

  /**
   * 获取销售信息
   */
  public getSaleDataStatistics() {
    this.service.getSaleDataStatistics(this.pageData.orgUserId).takeUntil(this.ngUnsubscribe).subscribe(res => {
      this.toolsService.hideLoading();
      if (this.checkResult(res)) {
        this.saleInfo = res.data;
      }
    });
  }

  /**
   * 跳转添加客户
   */
  public jumpTo() {
    this.router.navigate(['/sale/custom']);
  }

  /**
   *检查返回状态
   */
  private checkResult(res) {
    if (res.result !== '0') {
      this.toolsService.showToast(res.msg);
    }
    return res.result === '0';
  }

  ngOnInit() {
    this.toolsService.showLoading();
    this.route.queryParams.subscribe(params => {
      this.pageData.orgUserId = params['authOrgId'];
      this.authService.setAuthOrgId(this.pageData.orgUserId);
      this.getSaleDataStatistics();
    });
  }
}
